package com.supertange.miniapp.wepet.common.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 *  文件上传处理工具类
 */
public class FileUploadUtil {

	/**
	 *  上传
	 * @date 2017年10月9日 下午9:41:56
	 * @param multipartFile 上传的文件
	 * @param rootPath 上传根路径
	 * @param uploadPath 上传路径
	 * @return
	 * @throws Exception
	 */
	public static String upload(MultipartFile multipartFile, String rootPath, String uploadPath) throws Exception{
		Assert.notNull(multipartFile, "请上送文件");
		Assert.notNull(uploadPath,"请上送上传文件路径");
		// 原始名称
		String originalFilename = multipartFile.getOriginalFilename();
		// 上传文件
		if (originalFilename != null && originalFilename.length() > 0) {
			uploadPath = uploadPath + "/" + DateUtil.getCurrentDate("yyyyMMdd") + "/";
			// 判断文件夹是否存在，若不存在则创建文件夹
			if (!new File(rootPath + uploadPath).exists()) {
				new File(rootPath + uploadPath).mkdirs();
			}
			// 生成新文件名称
			String newFileName = UUID.randomUUID().toString().replace("-", "") + originalFilename.substring(originalFilename.lastIndexOf("."));
			File newFile = new File(rootPath + uploadPath + newFileName);
			// 将内存中的数据写入磁盘
			multipartFile.transferTo(newFile);
			// 将新图片名称返回
			return uploadPath + newFileName;
		}
		return null;
	}

	/**
	 * 七牛上传文件
	 * @param file 文件
	 * @param accessKey accessKey
	 * @param secretKey secretKey
	 * @param bucket bucket
	 * @param host 域名
	 * @param uploadType 上传文件类型
	 * @return
	 * @throws IOException
	 */
	public static String qnUpload(MultipartFile file,String accessKey,String secretKey,String bucket,String host,String uploadType) throws IOException {
        Assert.notNull(file, "文件未找到");
        //查询文件类型
        String completePath = "";
        //原始名称带拓展名
        String originalFilename = file.getOriginalFilename();
        //取文件扩展名
        String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        //创建上传工具
        QnUtil qnUtil = new QnUtil().create(accessKey, secretKey);
        qnUtil.setUploadManager(bucket);
        //生成存储区别名称
        UUID uuid = UUID.randomUUID();
        //生成上传目录
        String storageName = DateUtil.getCurrentDate("yyyyMMdd") + "/"+uploadType+"/"+uuid + "." + extName;
        qnUtil.uploadFile(bucket, null, file.getBytes(), storageName);
        //拼装完整访问目录
        completePath = host + storageName;
        return completePath;
    }

	/**
	 * 获取视频的封面
	 * @param videoPath 视频路径
	 * @param accessKey accessKey
	 * @param secretKey secretKey
	 * @param bucket bucket
	 * @param uploadType 上传类型
	 * @param coverType 封面图片格式
	 * @return
	 */
	public static String qnPathCover(String videoPath,String accessKey,String secretKey,String bucket,String uploadType,String coverType) {
		//创建上传工具
		QnUtil qnUtil = new QnUtil().create(accessKey, secretKey);
		qnUtil.setOperationManager(bucket);

		//生成存储区别名称
		UUID uuid = UUID.randomUUID();
		//生成上传目录
		String storageName = DateUtil.getCurrentDate("yyyyMMdd") + "/"+uploadType+"/"+uuid + "." + coverType;

		String completePath= qnUtil.getVideoCover(bucket,videoPath,storageName);

		return completePath;
	}

	/**
	 * 视频上传加水印
	 * @param videoPath 原视频路径
	 * @param accessKey accessKey
	 * @param secretKey secretKey】
	 * @param bucket bucket
	 * @param uploadType 上传类型
	 * @param coverType 转换视频格式
	 * @param base64Water base64格式水印图
	 * @return
	 */
	public static String qnVideoAddWater(String videoPath,String accessKey,String secretKey,String bucket,String uploadType,String coverType,String base64Water){
		//创建上传工具
		QnUtil qnUtil = new QnUtil().create(accessKey, secretKey);
		qnUtil.setOperationManager(bucket);

		//生成存储区别名称
		UUID uuid = UUID.randomUUID();
		//生成上传目录
		String storageName = DateUtil.getCurrentDate("yyyyMMdd") + "/"+uploadType+"/"+uuid + "." + coverType;


		String completePath= qnUtil.qnVideoAddImageWater(bucket,videoPath,storageName,base64Water);
		return completePath;
	}

	public static void qnDeleteVideo(String[] keys,String accessKey,String secretKey,String bucket){
		//创建上传工具
		QnUtil qnUtil = new QnUtil().create(accessKey, secretKey);
		qnUtil.deletes(bucket,keys);
	}




}
